﻿#region << 文件说明 >>
/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.ScheduledTask
 * 唯一标识：aadec90c-af5a-4f34-af63-0d3e6889b006
 * 文件名：ScheduledTaskExtension
 * 
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/8/27 14:06:52
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 文件说明 >>

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Vampirewal.Core.ScheduledTask;

/// <summary>
/// 
/// </summary>
public static class ScheduledTaskExtension
{

    /// <summary>
    /// 将定时任务注册到IOC服务
    /// </summary>
    /// <param name="services"></param>
    /// <param name="option"></param>
    /// <returns></returns>
    public static IServiceCollection AddVampirewalCoreScheduledTask(this IServiceCollection services, Action<IScheduledTaskOption> option) 
    {
        ScheduledTaskOption st=new ScheduledTaskOption(services);
        option?.Invoke(st);

        services.AddSingleton(st);

        services.AddSingleton<IVampirewalCoreScheduledTaskService, VampirewalCoreScheduledTaskService>();

        return services;
    }

    /// <summary>
    /// 开启定时任务
    /// </summary>
    /// <param name="app"></param>
    public static void UseVampirewalCoreScheduledTask(this IApplicationBuilder app)
    {
        var scope= app.ApplicationServices.CreateScope();
        var taskService= scope.ServiceProvider.GetRequiredService<IVampirewalCoreScheduledTaskService>();

        if (taskService != null) 
        {
            taskService.Running();
        }

    }
}